package com.yanglei.client;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.Inet4Address;
import java.net.ServerSocket;
import java.net.Socket;

public class ClienReceiveFileThread extends Thread {

	private ClientConnection clientConnetction;

	private FileOutputStream fileOutStream;

	private DataInputStream dateInputStream;

	private Socket socket;

	private ServerSocket serverSocket;

	private int port;

	public ClienReceiveFileThread(ClientConnection clientConnetction, int port) {
		super();
		this.clientConnetction = clientConnetction;
		this.fileOutStream = this.clientConnetction.getFileOutStream();
		this.dateInputStream = this.clientConnetction.getDateInputStream();

		this.port = port;
		try {
			this.serverSocket = new ServerSocket(
					this.clientConnetction.getPort1());

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	public void run() {
		try {
			socket = this.serverSocket.accept();
			this.fileOutStream = new FileOutputStream(
					this.clientConnetction.getFilePath());

			this.dateInputStream = new DataInputStream(
					this.socket.getInputStream());

			byte[] b = new byte[1];
			while (this.dateInputStream.read(b) != -1)
				this.fileOutStream.write(b);
		} catch (Exception e) {
			// TODO: handle exception
		}
	}

}
